package com.itheima.web;



import com.itheima.utils.FileUtil;
import com.itheima.utils.RedisUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeUnit;


@RestController
public class UploadController  {



    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        String key = FileUtil.upload(multipartFile.getInputStream(), multipartFile.getOriginalFilename());

        //set集合操作，将最新的文件放入到临时垃圾桶里面
        //设置value值，可以是多个：redisTemplate.opsForSet().add(String key,Object… values)
        //redisTemplate.opsForSet().add("temp-rubbish",key);
        RedisUtil.addToSet("temp-rubbish",key);

        //辅助key ，针对文件元素设置超时时间。
        //String类型：设置key和Value：redisTemplate.opsForValue().set(String key,Object value)
        //redisTemplate.opsForValue().set("file:"+key,1,2L, TimeUnit.MINUTES);
        RedisUtil.set("file:"+key,1,2L, TimeUnit.MINUTES);

        return key;

    }



}
